<template>
  <div class="collectArticle">
    <van-icon
      :name='value ? "star" : "star-o"'
      :color='value ? "#ffa500" : ""'
      @click="onCollect"
      :loading='loading'
    />
  </div>
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: 'CollectArticle',
  components: {},
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [String, Number, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  computed: {},
  watch: {},
  created () { },
  methods: {
    async onCollect () {
      this.loading = true
      try {
        if (this.value) {
          await deleteCollect(this.articleId)
        } else {
          await addCollect(this.articleId)
        }

        // 更新视图
        this.$emit('input', !this.value)
        // 提示
        this.$toast.success(!this.value ? '收藏成功' : '取消收藏')
      } catch (err) {
        this.$toast('失败请稍后重试')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped>
</style>
